package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

import java.util.Calendar;

@Repository("DetectDAO")
@Transactional
public class DetectDAOImpl extends AbstractJpaDao<Detect> implements DetectDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detect.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public DetectDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detect findDetectById(Integer id) throws DataAccessException {

		return findDetectById(id, -1, -1);
	}

	@Transactional
	public Detect findDetectById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectById", startResult, maxRows, id);
			return (Detect) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detect> findDetectByName(String name) throws DataAccessException {

		return findDetectByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findDetectByName", startResult, maxRows, name);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByNameContaining(String name) throws DataAccessException {

		return findDetectByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByDate(Calendar date) throws DataAccessException {

		return findDetectByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByDate(Calendar date, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findDetectByDate", startResult, maxRows, date);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByConclusion(String conclusion) throws DataAccessException {

		return findDetectByConclusion(conclusion, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByConclusion(String conclusion, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectByConclusion", startResult, maxRows, conclusion);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByConclusionContaining(String conclusion) throws DataAccessException {

		return findDetectByConclusionContaining(conclusion, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByConclusionContaining(String conclusion, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectByConclusionContaining", startResult, maxRows, conclusion);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByDescription(String description) throws DataAccessException {

		return findDetectByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Set<Detect> findDetectByDescriptionContaining(String description) throws DataAccessException {

		return findDetectByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@Transactional
	public Detect findDetectByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detect findDetectByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectByPrimaryKey", startResult, maxRows, id);
			return (Detect) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detect> findDetectsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detect> findAllDetects() throws DataAccessException {

		return findAllDetects(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findAllDetects(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetects", startResult, maxRows);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detect> findDetectsByDetectdepartment(Integer detectdepartmentId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectsByDetectdepartment", start, max, detectdepartmentId);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detect> findDetectsByFoodbatch(Integer foodbatchId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectsByFoodbatch", start, max, foodbatchId);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detect> findDetectsByDetector(Integer userId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectsByDetector", start, max, userId);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detect> findDetectsBySubject(Integer subjectId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectsBySubject", start, max, subjectId);
		return new LinkedHashSet<Detect>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detect> findDetectByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detect>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detect> findDetectsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detect.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Detect> detects = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detects;
	}

	@Transactional
	public Detect findDetectByPhysicalPrimaryKey(Integer subjectId, Integer foodbatchId, Calendar date,
			Integer detectorId, Integer detectdepartmentId) throws DataAccessException {
		return findDetectByPhysicalPrimaryKey(subjectId, foodbatchId, date, detectorId, detectdepartmentId, -1, -1);
	}

	@Transactional
	public Detect findDetectByPhysicalPrimaryKey(Integer subjectId, Integer foodbatchId, Calendar date,
			Integer detectorId, Integer detectdepartmentId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectByPhyscialPrimaryKey", startResult, maxRows, subjectId,
					foodbatchId, date, detectorId, detectdepartmentId);
			return (Detect) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detect entity) {
		return true;
	}
}
